//
//  NSMAssetBrowserCell.m
//  BSProject
//
//  Created by nsm on 2019/11/25.
//  Copyright © 2019 beisheng. All rights reserved.
//

#import "NSMAssetBrowserCell.h"

@interface NSMAssetBrowserCell () <UIScrollViewDelegate, UIGestureRecognizerDelegate>
@property(nonatomic, strong) UIScrollView *scrollView;
@end

@implementation NSMAssetBrowserCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self didInitialize];
    }
    return self;
}

- (void)didInitialize {
    _minimumZoomScale = 0.5;
    _maximumZoomScale = 2;
    self.contentMode = UIViewContentModeCenter;

    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scrollView.delegate = self;
    _scrollView.minimumZoomScale = _minimumZoomScale;
    _scrollView.maximumZoomScale = _maximumZoomScale;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11, *)) {
        _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_scrollView];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    singleTap.delegate = self;
    singleTap.numberOfTapsRequired = 1;
    singleTap.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:singleTap];
    
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGesture:)];
    doubleTap.numberOfTapsRequired = 2;
    doubleTap.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:doubleTap];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
    [self addGestureRecognizer:longPress];
    
    [singleTap requireGestureRecognizerToFail:doubleTap];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
}

#pragma mark - 手势
- (void)handleSingleTapGesture:(UITapGestureRecognizer *)gestureRecognizer {
    CGPoint location = [gestureRecognizer locationInView:gestureRecognizer.view];
    if ([self.delegate respondsToSelector:@selector(singleTapInCell:location:)]) {
        [self.delegate singleTapInCell:self location:location];
    }
}

- (void)handleDoubleTapGesture:(UITapGestureRecognizer *)gestureRecognizer {
    CGPoint location = [gestureRecognizer locationInView:gestureRecognizer.view];
    if ([self.delegate respondsToSelector:@selector(doubleTapInCell:location:)]) {
        [self.delegate doubleTapInCell:self location:location];
    }
}

- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer {
    if ((gestureRecognizer.state == YYGestureRecognizerStateBegan) &&
        [self.delegate respondsToSelector:@selector(longPressInCell:)]) {
        [self.delegate longPressInCell:self];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(nonnull UITouch *)touch {
    if ([touch.view isKindOfClass:UISlider.class]) {
        return NO;
    }
    return YES;
}

#pragma mark - method/public
- (void)viewWillAppear {
    
}

- (void)viewDidDisappear {
    
}

#pragma mark - private


@end
